import time
from selenium.webdriver.support import expected_conditions as EC
from selenium.common import WebDriverException, InvalidArgumentException, TimeoutException
from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from common.functions import open_browser
from selenium.webdriver.support.ui import Select


class Base:
    def __init__(self, browser, timeout=10):
        self.driver = browser
        self.driver.implicitly_wait(timeout)

    def get(self, url):
        try:
            self.driver.get(url)
        except (WebDriverException, InvalidArgumentException):
            print("网址错误")

    def max_window(self):
        self.driver.maximize_window()

    def switch_to_window(self):
        self.driver.switch_to.window(self.driver.window_handles[0])

    # 切换到frame标签

    def forward(self):
        self.driver.forward()

    def back(self):
        self.driver.back()

    def refresh(self):
        self.driver.refresh()

    def close(self):
        self.driver.close()

    def find_element(self, locator, timeout=5):
        try:
            ele = WebDriverWait(self.driver, timeout).until(EC.presence_of_element_located(locator))
            # print(ele.get_attribute("outerHTML"))
            return ele
        except TimeoutException:
            print("元素不存在")

    def find_element_text(self, locator):
        if self.find_element(locator):
            return self.find_element(locator).text
        else:
            print("元素不存在")

    def click(self, locator):
        if self.find_element(locator):
            self.find_element(locator).click()
        else:
            print("元素不存在")

    def send_keys(self, locator, text):
        if self.find_element(locator):
            self.find_element(locator).send_keys(text)
        else:
            print("元素不存在")

    def clear(self, locator):
        if self.find_element(locator):
            self.find_element(locator).clear()
        else:
            print("元素不存在")

    def get_attr(self, locator, attr):
        if self.find_element(locator):
            return self.find_element(locator).get_attribute(attr)
        else:
            print("元素不存在")

    def select_by_index(self, locator, index):
        ele = self.find_element(locator)
        if ele:
            select = Select(ele)
            select.select_by_index(index)
        else:
            print("元素不存在")

    def select_by_value(self, locator, value):
        ele = self.find_element(locator)
        if ele:
            select = Select(ele)
            select.select_by_index(value)
        else:
            print("元素不存在")

    def select_by_text(self, locator, text):
        ele = self.find_element(locator)
        if ele:
            select = Select(ele)
            select.select_by_visible_text(text)
        else:
            print("元素不存在")

    def is_displayed(self, locator):
        if self.find_element(locator):
            return self.find_element(locator).is_displayed()
        else:
            print("元素不存在")

    def is_enabled(self, locator):
        if self.find_element(locator):
            return self.find_element(locator).is_enabled()
        else:
            print("元素不存在")

    # def switch_to_frame(self):
    #     if self.find_element():
    #         self.driver.switch_to.frame(self.find_element())
    #     else:
    #         print("元素不存在")

    # def switch_to_parent_frame(self, locator):
    #     if self.find_element(locator):
    #         self.driver.switch_to.parent_frame(self.find_element(locator))
    #     else:
    #         print("元素不存在")

    def switch_to_default_content(self):
        self.driver.switch_to.default_content()


    def quit(self, seconds=3):
        time.sleep(seconds)
        self.driver.quit()


if __name__ == '__main__':
    t1 = ("id", "kw")
    t2 = ("id", "su")
    browser = open_browser()
    base = Base(browser)
    base.get("https://www.baidu.com")
    base.get_attr(t1, "innerHTML")
    base.send_keys(t1, "python")
    base.find_element_text(t2)
    base.clear(t1)
    base.send_keys(t1, "天气预报")
    base.click(t2)
    base.back()
    base.forward()
    base.refresh()
    base.quit()
